# coding=utf-8
"""
    @project: 15python_spider
    @Author：frank
    @file： 08_doubanmovie_ajax.py
    @date：2024/2/20 21:13
"""
import requests


class DoubanSpider:
    def __init__(self):
        self.url = 'https://movie.douban.com/j/chart/top_list?type={}&interval_id=100%3A90&action=&start=0&limit={}'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0',
            'Accept': '*/*',
            'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
            # 慎用：是否支持压缩，浏览器会自动解压缩，程序不会
            # 'Accept-Encoding': 'gzip, deflate, br',
            'Cookie': 'll="118200"; bid=r8-XHTkMQCw; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1708434925%2C%22https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DGf-qCA1rwWxAbSvaF34AGO94bQDPGylr3VGFwr1L0IQ7qvAoY6nD1f7ujFRm57Qo%26wd%3D%26eqid%3Dcd2d6638000006df0000000665d4a5e7%22%5D; _pk_id.100001.4cf6=c133961d03bdef23.1708434925.; ap_v=0,6.0; __yadk_uid=hjH8bNlZRgYJZOOf9GXWsb36y28BAts7; __utma=30149280.1004464390.1708434928.1708434928.1708434928.1; __utmc=30149280; __utmz=30149280.1708434928.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=223695111.882859032.1708434928.1708434928.1708434928.1; __utmc=223695111; __utmz=223695111.1708434928.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)'

        }

    def get_film_info(self, url):
        # res.json() -> 返回python数据类型（字典或列表）
        html_json = requests.get(url=url, headers=self.headers).json()

        for film in html_json:
            film_name = film['title']
            film_score = film['score']
            print(film_name, film_score)

    def main(self):
        type_dict = {'剧情': '11', '喜剧': '24', '爱情': '13'}
        film_type = input('请输入电影类型(剧情|喜剧|爱情):')
        if film_type in type_dict:
            # 查询参数中具体的type值
            ty = type_dict[film_type]
            limit = input('请输入电影数量:')
            url = self.url.format(ty, limit)
            self.get_film_info(url)
        else:
            print('类型不存在')


if __name__ == "__main__":
    spider = DoubanSpider()
    spider.main()
